package com.atguigu.tingshu.user.api;

import cn.hutool.core.lang.Assert;
import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "用户管理接口")
@RestController
@RequestMapping("api/user")
@SuppressWarnings({"all"})
public class UserInfoApiController {

	@Autowired
	private UserInfoService userInfoService;

	/**
	 * 根据用户ID获取用户（主播）基本信息
	 * @param userId
	 * @return
	 */
	@GetMapping("/userInfo/getUserInfoVo/{userId}")
	public Result<UserInfoVo> getUserInfoVoById(@PathVariable("userId") Long userId){
		UserInfoVo userInfoVo = userInfoService.getUserInfoVoById(userId);
		return Result.ok(userInfoVo);
	}

	///api/user/userInfo/userIsPaidTrack/{userId}/{albumId}
	@PostMapping("/userInfo/userIsPaidTrack/{userId}/{albumId}")
	public Result<Map<Long,Integer>> userIsPaidTrack(
			@PathVariable Long userId,
			@PathVariable Long albumId,
			@RequestBody List<Long> needChackTrackIdList
			) {
		Map<Long,Integer> map = userInfoService.userIsPaidTrack(userId,albumId,needChackTrackIdList);
		return Result.ok(map);
	}
	///api/user/userInfo/isPaidAlbum/{albumId}
	@GuiGuLogin
	@GetMapping("/userInfo/isPaidAlbum/{albumId}")
	public Result<Boolean> isPaidAlbum(@PathVariable Long albumId){
		Long userId = AuthContextHolder.getUserId();
		Assert.notNull(userId,"用户未登录");
		Boolean flag = userInfoService.isPaidAlbum(userId,albumId);
		return Result.ok(flag);
	}

	///api/user/userInfo/findUserPaidTrackList/{albumId}
	@GuiGuLogin
	@GetMapping("/userInfo/findUserPaidTrackList/{albumId}")
	public Result<List<Long>> findUserPaidTrackList(@PathVariable Long albumId){
		Long userId = AuthContextHolder.getUserId();
		List<Long> list = userInfoService.findUserPaidTrackList(userId,albumId);
		return Result.ok(list);
	}

	@GuiGuLogin
	@PostMapping("/userInfo/savePaidRecord")
	public Result savePaidRecord(@RequestBody UserPaidRecordVo userPaidRecordVo) {
		//1.获取登录用户ID
		Long userId = AuthContextHolder.getUserId();
		if (userId != null) {
			userPaidRecordVo.setUserId(userId);
		}
		//2.调用业务逻辑处理用户购买记录
		userInfoService.savePaidRecord(userPaidRecordVo);
		return Result.ok();
	}

}

